package edu.pet.Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import edu.pet.DAO.PetDAO;
import edu.pet.entity.History;
import edu.pet.entity.Pet;

public class HistoryAddServlet extends HttpServlet {

	public HistoryAddServlet() {
		super();
	}


	public void destroy() {
		super.destroy(); 
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String handle = request.getParameter("handle");
		int petid = Integer.parseInt(request.getParameter("petid"));
		if(handle.equals("show")){
			Pet pet = new Pet();
			PetDAO petDao = new PetDAO();
			pet = petDao.searchPet(petid);
			request.setAttribute("pet", pet);
			response.sendRedirect("../user/addhistory.jsp");
		}
		if(handle.equals("add")){
			String date = request.getParameter("date");
			String description = request.getParameter("description");
			History history = new History();
			history.setDate(date);
			history.setDescirption(description);
			PetDAO petDao = new PetDAO();
			petDao.addHistory(history, petid);
			response.sendRedirect("../servlet/PetServlet?id="+petid);
		}
	}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doGet(request, response);
	}

	public void init() throws ServletException {
		// Put your code here
	}

}
